package com.pcbly.web.persistence.dict;

import java.util.List;

import com.pcbly.web.bean.dict.DictCategoryBean;
import com.pcbly.web.bean.dict.DictCategoryDependBean;
import com.pcbly.web.persistence.HibernateBaseDao;

@SuppressWarnings("unchecked")
public class DictCategoryDependHibernateDao extends
		HibernateBaseDao<DictCategoryDependBean> implements
		DictCategoryDependDao {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5567608807071858450L;

	public DictCategoryDependHibernateDao() {
		super();
	}

	protected Class<DictCategoryDependBean> getEntityClass() {
		return DictCategoryDependBean.class;
	}

	@Override
	public void removeDictCategoryDependByDictId(String id) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
		sb.append("delete c from ");
		sb.append(" sys_dict_depend c ");
		sb.append(" where ");
		sb.append(" c.MASTERID in('");
		sb.append(id.replaceAll(",", "','"));
		sb.append("') or  c.CHILDID in('");
		sb.append(id.replaceAll(",", "','"));
		sb.append("')");
		sb.append("");
		getSession().createSQLQuery(sb.toString()).executeUpdate();
//		DictCategoryDependBean entity = new DictCategoryDependBean();
//		entity.setMasterId(id);
//		getHibernateTemplate().delete(entity);
//		entity = new DictCategoryDependBean();
//		entity.setChildId(id);
//		getHibernateTemplate().delete(entity);

	}

	@Override
	public List<DictCategoryBean> queryDictCategoryBeansByMasterId(
			String masterId) throws Exception {
		// TODO Auto-generated method stub
		StringBuilder sb=new StringBuilder();
//		sb.append("select a.id,a.name,a.desc,a.userDefined,a.orderBy ");
		sb.append("select a ");
		sb.append(" from DictCategoryBean a,DictCategoryDependBean b  ");
		sb.append(" where a.id=b.childId and b.masterId=? "); 
		return getHibernateTemplate().find(sb.toString(), masterId);
	}

}
